export function formatTime(time) {
  if(!time) {
    return ''
  }
	// 先把传参毫秒转化为new Date()
	const dt = new Date(time)
	const y = dt.getFullYear()
	// 月份是从0开始,需要+1。  +''是把数字转化为字符串,padStart(2,'0')是把字符串设置为2位数,不足2位则在开头加'0'
	const m = (dt.getMonth() + 1 + '').padStart(2, '0')
	const d = (dt.getDate() + '').padStart(2, '0')
	const hh = (dt.getHours() + '').padStart(2, '0')
	const mm = (dt.getMinutes() + '').padStart(2, '0')
	const ss = (dt.getSeconds() + '').padStart(2, '0')
	return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

export function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
*/
export function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

// 判断是不是对象
export function isBoj(obj) {
	return Object.prototype.toString.call(obj) === '[object Object]'
}

// 判断是不是数组
export function isArrayFn(value){
	if(typeof Array.isArray === "function") {
		return Array.isArray(value);
	} else {
		return Object.prototype.toString.call(value) === "[object Array]";
	}
}

export function phoneReg(phone) {
	var myreg = /^[1][3-9][0-9]{9}$/;
	return myreg.test(phone)
}

//防抖
export function debounce(fn, wait = 500) {
	var timer = null;
	return function() {
		var context = this
		var args = arguments
		if (timer) {
			clearTimeout(timer);
			timer = null;
		}
		timer = setTimeout(function() {
			fn.apply(context, args)
		}, wait)
	}
}


// 节流 默认1000毫秒
export function throttleFunc(func, marginTime) {
	if (marginTime == undefined || marginTime == null) {
		marginTime = 1000
	}
	let lastTime = null
	return async function() {
		let currentTime = +new Date()
		if (currentTime - lastTime > marginTime || !lastTime) {
			await func.apply(this, arguments)
			lastTime = currentTime
		}
	}
}
